<?php
namespace Android\Model;

class ClassModel extends BaseModel {


    /**
     * 更具指定条件查询班级
     *
     * @param $where
     * @param string $limit
     * @param bool $fullname 是否查询班级完整名称
     * @return mixed
     */
    public function getClassByWhere($where,$limit='',$fullname=false) {
        is_null($where) && $where = '';
        $res = $this->where($where)->limit($limit)->select();

        if (!$fullname) return $res;
        foreach ($res as $k=>$v) {
            $res[$k]['name'] = $this->getFullClassName($v['id']);
        }

        return $res;
    }


    public function getCountByWhere($where) {
        is_null($where) && $where = '';
        return $this->where($where)->count();
    }


    public function getClassName($id) {
        return $this->where('id = '.$id)->getField('name');
    }


    /**
     * 获取完整的班级名称,包含专业名称
     *
     * @param $id
     * @return null|string
     */
    public function getFullClassName($id) {
        $c = $this->find($id);
        if (empty($c)) return null;

        $Profession = M ('Profession');
        $pName = $Profession->where('id = '.$c['profession_id'])->getField('name');

        return $pName . ' ' . $c['name'];
    }



    public function getProfessionId ($id) {
        return $this->where('id = '.$id)->getField('profession_id');
    }



    public function getClassArrByIdArr($idArr) {
        $arr = array();
        if (empty($idArr)) return $arr;
        foreach ($idArr as $v) {
            $c = $this->find($v);
            !empty($c) && $c['name'] = $this->getFullClassName($v);
            $arr[] = $c;
        }
        return $arr;
    }

    // 修改班级组织信息
    public function editClassInfo($data) {
        if (($flag = $this->save($data)) !== false) return '000000';
        return '111111';
    }

}